/**
 * Created by Eric.Dou on 4/4/14.
 */
define(function (require) {
    var utils = require("lib/utils");

    return (function OptionsBuilder($, options, functions) {
        this.returnObject = [];
        this.args = {};
        this.functionArray = [];
        var self = this,_funcs=functions;

        this.addData = function (data) {
            if (!$.isEmptyObject(data)) {
                $.extend(self.args, data);
            }
            return self;
        };

        this.clear = function () {
            self.args = {};
            self.returnObject = [];
        };
        this.clearFns = function () {
            self.functionArray = [];
        };

        this.addAction = function (action) {
            if ($.isFunction(action) || typeof(action) == "object") {
                self.functionArray.push(action);
            } else if ($.isArray(action)) {
                $.each(action, function (i, data) {
                    self.addAction(data);
                });
            }
            return self;
        };

        this.pushData = function (data) {
            return self.returnObject.push(data);
        };

        this.popData = function () {
            return self.returnObject.pop();
        };

        function _init(args) {
            if (typeof(args.length) == "undefined") {
                return self;
            }
            else {
                var newArray = [];
                if (typeof(options) == "function") {
                    newArray.push(options);
                    options = null;
                }
                var fnArray = [];
                if (args.length >= 2) {
                    if (typeof(_funcs) == "function") {
                        newArray.push(_funcs)
                    }
                    else if ($.isArray(_funcs)) {
                        fnArray = _funcs;
                    }
                    //the after arguments element insert into functions .
                    var afterArgs = [];
                    for (i = 2; i < args.length; i++) {
                        afterArgs.push(args[i]);
                    }
                    _funcs = utils.arrayMerge(newArray, fnArray, afterArgs);
                }
            }
            if ($.isEmptyObject(_funcs)) {
                return self;
            }
            self.addAction(_funcs);
            self.addData(options);
        }

        this.builder = function (options, action) {
            if (typeof  action == "undefined") {
                return;
            }
            $.each(action, function (i, data) {
                if ($.isArray(data)) {
                    self.builder(options, data);
                }
                else if (typeof(data) == "object") {
                    self.pushData(data);
                }
                else if (typeof(data) == "function") {
                    var returnObj = data(options);
                    if (typeof returnObj != "undefined") {
                        self.pushData(returnObj);
                    }
                }
            });
        };
        this.reload = function () {
            if ($.isEmptyObject(self.args)) {
                return;
            }
            self.returnObject = new Array();
            self.builder(self.args, self.functionArray);
            return self;
        };

        this.result = function () {
            return self.returnObject;
        };

        _init(arguments);

        return  self;
    })(jQuery, options, functions);
});